using System.Collections.Generic;
using Wolfpack.Periscope.Core.Extensions;

namespace Wolfpack.Periscope.Core.Interfaces.Entities
{
    public class WidgetInstance : IWidgetLayout
    {
        //private IDictionary<string, string> _additionalTemplateData;

        public string RendererId { get; set; }
        public string CssTemplate { get; set; }
        public string MarkupTemplate { get; set; }
        public string ScriptTemplate { get; set; }
        public string SampleTemplate { get; set; }
        public string TemplatesFolder { get; set; }
        public List<Property> ScriptIncludes { get; set; }
        public List<Property> StyleSheetIncludes { get; set; }

        public string Id { get; set; }
        public int Row { get; set; }
        public int Column { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }

        public Dictionary<string, string> TemplateData { get; set; }

        //public Dictionary<string, string> TemplateData
        //{
        //    get
        //    {
        //        var templateData = new Dictionary<string, string>(_additionalTemplateData);
        //        return templateData.AddFromAnonymous(ImportantProperties());
        //    }
        //    set
        //    {
        //        _additionalTemplateData = value;
        //        _additionalTemplateData.RemoveFromAnonymous(ImportantProperties());
        //    }
        //}

        private object ImportantProperties()
        {
            return new {};

            return new
            {
                Id,
                Row,
                Column,
                Width,
                Height,
                Title,
                Description
            };
        }

        public WidgetInstance()
        {
            Row = 1;
            Column = 1;
            Height = 250;
            Width = 250;
            ScriptIncludes = new List<Property>();
            StyleSheetIncludes = new List<Property>();

            TemplateData = new Dictionary<string, string>();
        }

        public WidgetInstance UseTemplatePack(string name, string templateBaseFolder = null)
        {
            TemplatesFolder = templateBaseFolder;
            ScriptTemplate = name;
            CssTemplate = name;
            MarkupTemplate = name;
            SampleTemplate = name;
            return this;
        }
        public WidgetInstance AddTemplateData(object anonymous)
        {
            //_additionalTemplateData.AddFromAnonymous(anonymous);
            TemplateData.AddFromAnonymous(anonymous);
            return this;
        }
        public WidgetInstance RegisterScript(string name, string link)
        {
            ScriptIncludes.Add(new Property { Name = name, Value = link });
            return this;
        } 
        
        public WidgetInstance RegisterStyleSheet(string name, string link)
        {
            StyleSheetIncludes.Add(new Property { Name = name, Value = link });
            return this;
        }
        public WidgetInstance RenderWith<T>()
        {
            RendererId = typeof (T).Name;
            return this;
        }
        public WidgetInstance RenderWith(string rendererId)
        {
            RendererId = rendererId;
            return this;
        }

        public void UpdateLayout(IWidgetLayout widgetLayout)
        {
            Row = widgetLayout.Row;
            Column = widgetLayout.Column;
            Width = widgetLayout.Width;
            Height = widgetLayout.Height;
        }
    }
}